home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / plnk081.zip / pilot-link.0.8.1 / Java / Pdapilot / DB.java < prev    next >
Text File  |  1997-08-03  |  6KB  |  221 lines

  1.  
  2. package Pdapilot;
  3.  
  4. import java.io.*;
  5.  
  6. /** A representation of an open database.
  7.  */
  8.  
  9. public class DB {
  10.         Dlp socket;
  11.         int handle;
  12.         String dbname;
  13.         int dbmode;
  14.         int dbcard;
  15.         
  16.         public Database dbClass;
  17.         
  18.         DB(Dlp socket, int handle, String dbname, int dbmode, int dbcard) {
  19.             this.socket = socket;
  20.             this.handle = handle;
  21.             this.dbname = dbname;
  22.             this.dbmode = dbmode;
  23.             this.dbcard = dbcard;
  24.             
  25.             System.out.println("Opening database '"+dbname+"'");
  26.             dbClass = (Database)Database.dbClasses.get(dbname);
  27.             if (dbClass == null) {
  28.                 dbClass = Database.defaultDbClass;
  29.                 System.out.println("Using default dbclass");
  30.             }
  31.         }
  32.  
  33.         DB(Dlp socket, int handle, String dbname, int dbmode, int dbcard, Database dbClass) {
  34.             this.socket = socket;
  35.             this.handle = handle;
  36.             this.dbname = dbname;
  37.             this.dbmode = dbmode;
  38.             this.dbcard = dbcard;
  39.             this.dbClass = dbClass;
  40.             
  41.             System.out.println("Opening database '"+dbname+"'");
  42.         }
  43.         
  44.         public void close() throws DlpException { 
  45.             /* this method should be idempotent */
  46.             if (handle != 0)
  47.                 socket.dlp_CloseDB(handle);
  48.             handle = 0;
  49.         }
  50.         
  51.         protected void finalize() throws DlpException {
  52.             this.close();
  53.         }
  54.         
  55.         public AppBlock getAppBlock() throws DlpException {
  56.             return socket.dlp_ReadAppBlock(handle, dbClass);
  57.         }
  58.         
  59.         public AppBlock newAppBlock() {
  60.             return dbClass.newAppBlock();
  61.         }
  62.  
  63.         public SortBlock getSortBlock() throws DlpException {
  64.             return socket.dlp_ReadSortBlock(handle, dbClass);
  65.         }
  66.  
  67.         public SortBlock newSortBlock() {
  68.             return dbClass.newSortBlock();
  69.         }
  70.  
  71.         public Record getRecord(int index) throws DlpException {
  72.             return socket.dlp_ReadRecordByIndex(handle, index, dbClass);
  73.         }
  74.  
  75.         public Record newRecord() {
  76.             return dbClass.newRecord();
  77.         }
  78.  
  79.         public Record newRecord(RecordID id) {
  80.             return dbClass.newRecord(id);
  81.         }
  82.  
  83.         public Resource newResource() {
  84.             return dbClass.newResource();
  85.         }
  86.  
  87.         public Resource newResource(Char4 type, int id) {
  88.             return dbClass.newResource(type, id);
  89.         }
  90.         
  91.         public Record getRecord(RecordID id) throws DlpException {
  92.             return socket.dlp_ReadRecordByID(handle, id, dbClass);
  93.         }
  94.  
  95.         public Record getNextRecord(int category) throws DlpException {
  96.             return socket.dlp_ReadNextRecInCategory(handle, category, dbClass);
  97.         }
  98.  
  99.         public Record getNextModRecord() throws DlpException {
  100.             return socket.dlp_ReadNextModifiedRec(handle, dbClass);
  101.         }
  102.  
  103.         public Record getNextModRecord(int category) throws DlpException {
  104.             return socket.dlp_ReadNextModifiedRecInCategory(handle, category, dbClass);
  105.         }
  106.         
  107.         public Resource getResource(int index) throws DlpException {
  108.             return socket.dlp_ReadResourceByIndex(handle, index, dbClass);
  109.         }
  110.  
  111.         public Resource getResource(Char4 type, int id) throws DlpException {
  112.             return socket.dlp_ReadResourceByType(handle, type, id, dbClass);
  113.         }
  114.         
  115.         public long setRecord(Record record) throws DlpException {
  116.             return socket.dlp_WriteRecord(handle, record);
  117.         }
  118.  
  119.         public void setResource(Resource resource) throws DlpException {
  120.             socket.dlp_WriteResource(handle, resource);
  121.         }
  122.         
  123.         public void setAppBlock(AppBlock appblock) throws DlpException {
  124.             socket.dlp_WriteAppBlock(handle, appblock);
  125.         }
  126.  
  127.         public void setSortBlock(SortBlock sortblock) throws DlpException {
  128.             socket.dlp_WriteSortBlock(handle, sortblock);
  129.         }
  130.  
  131.         public void deleteRecord(RecordID id) throws DlpException {
  132.             socket.dlp_DeleteRecord(handle, false, id);
  133.         }
  134.  
  135.         public void deleteRecords() throws DlpException {
  136.             socket.dlp_DeleteRecord(handle, true, null);
  137.         }
  138.  
  139.         public void deleteResource(Char4 type, int id) throws DlpException {
  140.             socket.dlp_DeleteResource(handle, false, type, id);
  141.         }
  142.  
  143.         public void deleteResources() throws DlpException {
  144.             socket.dlp_DeleteResource(handle, true, null, 0);
  145.         }
  146.         
  147.         public void deleteCategory(int category) throws DlpException {
  148.             socket.dlp_DeleteCategory(handle, category);
  149.         }
  150.         
  151.         public void moveCategory(int from, int to) throws DlpException {
  152.             socket.dlp_MoveCategory(handle, from, to);
  153.         }
  154.  
  155.         public void purge() throws DlpException {
  156.             socket.dlp_CleanUpDatabase(handle);
  157.         }
  158.  
  159.         public void resetFlags() throws DlpException {
  160.             socket.dlp_ResetSyncFlags(handle);
  161.         }
  162.  
  163.         public void resetNext() throws DlpException {
  164.             socket.dlp_ResetDBIndex(handle);
  165.         }
  166.         
  167.         public int getRecords() throws DlpException {
  168.             return socket.dlp_ReadOpenDBInfo(handle);
  169.         }
  170.  
  171.         public Pref getPref(int id)
  172.             throws DlpException, IOException
  173.         {    return getPref(id, true); }
  174.         
  175.         public Pref getPref(int id, boolean backup)
  176.             throws DlpException, NoCreatorException, IOException
  177.         {
  178.             if (socket.version() < 0x101)
  179.                 socket.dlp_CloseDB(handle);
  180.             if (dbClass.creator() == null)
  181.                 throw new NoCreatorException();
  182.             Pref result = socket.dlp_ReadAppPreference(dbClass.creator(), id, backup, dbClass);
  183.             if (socket.version() < 0x101)
  184.                 handle = socket.dlp_OpenDB(dbcard, dbmode, dbname);
  185.             return result;
  186.         }
  187.         
  188.         public Pref newPref(int id)
  189.         {
  190.             return dbClass.newPref(null, dbClass.creator(), id, 1, true);
  191.         }
  192.         
  193.         public Pref newPref(int id, int version, boolean backup)
  194.         {
  195.             return dbClass.newPref(null, dbClass.creator(), id, version, backup);
  196.         }
  197.  
  198.         public void setPref(Pref pref)
  199.             throws DlpException, IOException
  200.         {
  201.             if (socket.version() < 0x101)
  202.                 socket.dlp_CloseDB(handle);
  203.             socket.dlp_WriteAppPreference(pref);
  204.             if (socket.version() < 0x101)
  205.                 handle = socket.dlp_OpenDB(dbcard, dbmode, dbname);
  206.         }
  207.  
  208.         public RecordID[] getRecordIDs()throws DlpException
  209.             { return getRecordIDs(false);    }
  210.         public RecordID[] getRecordIDs(boolean sort)     throws DlpException
  211.         {    return getRecordIDs(sort, 0);     }
  212.         public RecordID[] getRecordIDs(boolean sort, int start)        throws DlpException
  213.         {    return getRecordIDs(sort, start, 0xffff);    }
  214.         public RecordID[] getRecordIDs(boolean sort, int start, int max)
  215.             throws DlpException
  216.         {
  217.             return socket.dlp_ReadRecordIDList(handle, sort, start, max);
  218.         }
  219.  
  220. }
  221.